import router from '@/router'
import axios from 'axios'
import Vue from 'vue'
import { getCurrentInstance } from 'vue';
//axios实例对象
const instance = axios.create({
    baseURL: '/api',
    timeout: 100000
})

//请求拦截
instance.interceptors.request.use(config => {
    let token = sessionStorage.getItem('token')
    if (token) {
        config.headers = {
            Authorization: token
        }
    }
    return config
}, err => {
    return Promise.reject(err)
})

//响应拦截
instance.interceptors.response.use(res => {
    if (res.data.code == 401) { 
    router.push('/epidemicConsultation')
    sessionStorage.clear() 
    Vue.prototype.$message.error("权限过期，请重新登录")}
    // window.location.reload()
    return res
}, err => {
    return Promise.reject(err)
})

export default instance